package com.sharkpro.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sharkpro.entity.Materials;
import com.sharkpro.service.MaterialsService;
import com.sharkpro.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jianxin.wei
 * @description 学习资料控制器类，类似于论坛
 * @date 2024/12/31 13:31
 */
@RestController
@RequestMapping("materials")
@Slf4j
public class LearningMaterialsController {

    @Resource
    MaterialsService service;

    /**
     * 资料列表
     */
    @RequestMapping("list")
    public ResultUtil list(@RequestBody JSONObject param) {
        QueryWrapper<Materials> queryWrapper = new QueryWrapper<Materials>().eq("view_flag", 1);
        Page<Materials> page = new Page<>(param.getInt("pageNo"), 20);
        List<Materials> list = service.list(page, queryWrapper);
        JSONArray array = new JSONArray();
        list.forEach(material -> {
            JSONObject object = new JSONObject();
            object.set("id", material.getId()); // 该版本的文档ID
            object.set("essayId", material.getEssayId()); // 文档ID
            object.set("name", material.getName()); // 文档名称
            object.set("createTime", material.getCreateTime());
            object.set("version", material.getVersion()); // 版本号
            array.add(object);
        });
        log.info("Materials.list()的出参是:" + array);
        return new ResultUtil().success(array);
    }

    /**
     * 获取某文档的详情
     */
    @RequestMapping("info")
    public ResultUtil info(@RequestBody JSONObject param) {
        String id = param.getStr("id");
        Materials material = service.getById(id);
        log.info("Materials.info()的出参是:" + material);
        return new ResultUtil().success(material);
    }

    /**
     * 获取某文档的所有历史文档
     */
    @RequestMapping("historyList")
    public ResultUtil historyList(@RequestBody JSONObject param) {
        String essayId = param.getStr("essayId");
        QueryWrapper<Materials> queryWrapper = new QueryWrapper<Materials>().eq("essay_id", essayId);
        List<Materials> historyList = service.list(queryWrapper);
        log.info("Materials.historyList()的出参是:" + historyList);
        return new ResultUtil().success(historyList);
    }

    /**
     * 修改某文档的内容
     */
    @RequestMapping("update")
    public ResultUtil update(@RequestBody JSONObject param) {
        Materials entity = new Materials();
        entity.setEssayId(Integer.valueOf(param.getStr("essayId")));
        entity.setName(param.getStr("name"));
        entity.setContent(param.getBytes("content"));
        entity.setAccount(param.getStr("account"));
        entity.setVersion(Double.parseDouble(param.getStr("version")) + 0.1);
        service.save(entity);
        return new ResultUtil().success();
    }

}
